package com.tanhua.api.impl.mongo;

import com.tanhua.api.mongo.CommentApi;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    public Long tongyong(Comment comment) {
         //1 设置固定参数
        comment.setCreated(System.currentTimeMillis());
        //根据publishId查询到发布人的id
        Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
        comment.setPublishUserId(publish.getUserId());
        //2 保存
        mongoTemplate.insert(comment,"quanzi_comment");

        //3 修改数量
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),1); //点赞 评论 喜欢
        mongoTemplate.upsert(query,update,Publish.class);

        //4 查询最新数量返回
        Query qr = new Query(Criteria.where("pubType").is(comment.getPubType())
        .and("commentType").is(comment.getCommentType()).and("publishId").is(comment.getPublishId()));
        long count = mongoTemplate.count(qr, "quanzi_comment");

        return count; //点赞数 评论数 喜欢数
    }

    @Override
    public Long tongyong2(Comment comment) {

        //设置条件做删除
        Query removeQuery = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
                        .and("userId").is(comment.getUserId()).and("pubType").is(comment.getPubType()));
        mongoTemplate.remove(removeQuery,"quanzi_comment");

        //3 修改数量
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),-1); //点赞 评论 喜欢
        mongoTemplate.upsert(query,update,Publish.class);

        //4 查询最新数量返回
        Query qr = new Query(Criteria.where("pubType").is(comment.getPubType())
                .and("commentType").is(comment.getCommentType()).and("publishId").is(comment.getPublishId()));
        long count = mongoTemplate.count(qr, "quanzi_comment");

        return count; //点赞数 评论数 喜欢数

    }

    @Override
    public PageBeanVo findComment(String movementId,Integer page,Integer pagesize) {

        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(2).and("pubType").is(1));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(page,pagesize,count,list);

    }

    @Override
    public PageBeanVo findPageComment(Integer page, Integer pagesize, int commentId, Long userId) {

        Query query = new Query(Criteria.where("commentType").is(commentId).and("publishUserId").is(userId));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(page,pagesize,count,commentList);
    }
}
